/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.global.adapter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import cn.com.ksplatform.modules.autocode.dto.Propertys;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.util.SenceUtil;

/**
 * ClassName:Adapter_0_go.java <br/>
 * Date: 2015年11月18日 <br/>
 * 方法适配器
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Component
public class Adapter_1_script implements IMethodAdapter {
	
	@Override
	public void doAdapter(VelocityConfigurer config, 
			Sence senceModel, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		VelocityEngine ve = config.getVelocityEngine();
		Propertys pts = SenceUtil.getPropertys(senceModel);
		
		Map param = new HashMap();
		param.put("request", request);
		param.put("sence", senceModel);
		param.put("module", senceModel.getModule());
		param.put("propertys", pts.getPropertys());
		
		String tb = request.getParameter("tb");
		
		String script = null;
		if(StringUtils.hasText(tb)){
			if(tb.equals("main")){
				script = VelocityEngineUtils.mergeTemplateIntoString(ve,"cn/com/ksplatform/modules/autocode/template/page/zzb_main_script.vm","UTF-8",param);
			}else if(tb.equals("children")){
				param.put("cc", request.getParameter("cc"));
				script = VelocityEngineUtils.mergeTemplateIntoString(ve,"cn/com/ksplatform/modules/autocode/template/page/zzb_children_script.vm","UTF-8",param);
			}else{
				throw new Exception("tb只能为 main或者 children");
			}
		}
		response.setContentType("text/javascript");
		response.getWriter().write(script);
	}
}
